<template>
  <div>
    <div style="margin-bottom: 15px;">
      <el-input v-model="params.name" placeholder="请输入皮肤名称" style="width: 300px; "></el-input>
      <el-input v-model="params.author" placeholder="请输入皮肤作者" style="width: 300px; margin-left: 5px;"></el-input>
      <el-button style="margin-left: 10px;" @click="findBySearch">查询</el-button>
      <el-button style="margin-left: 10px;" @click="reset">清空</el-button>
      <el-button style="margin-left: 10px;" @click="add">新增</el-button>
    </div>
    <div>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="name" label="皮肤名称" width="180"></el-table-column>
        <el-table-column prop="author" label="皮肤作者" width="180"></el-table-column>
        <el-table-column prop="price" label="皮肤价格" width="180"></el-table-column>
        <el-table-column prop="press" label="皮肤出版社"></el-table-column>
        <el-table-column prop="img" label="皮肤封面">
          <template slot-scope="scope">
            <el-image
                style="width: 100px; height: 100px"
                :src="'http://localhost:8080/api/files/' + scope.row.img"
                :preview-src-list="['http://localhost:8080/api/files/' + scope.row.img]">
            </el-image>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" @click="edit(scope.row)" v-if="user.role === 'ROLE_ADMIN'">编辑</el-button>
            <el-button type="primary" @click="down(scope.row)">下载</el-button>
            <el-popconfirm title="确认删除该记录？" @confirm="del(scope.row.id)">
              <el-button slot="reference" type="danger" style="margin-left: 10px;" v-if="user.role === 'ROLE_ADMIN'">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block" style="margin-top: 15px;">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="params.pageNum"
          :page-sizes="[5,10,15,20]"
          :page-size="params.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
    <div>
      <el-dialog title="请填写信息" :visible.sync="dialogFormVisible" width="40%">
        <el-form :model="form">
          <el-form-item label="皮肤名称" label-width="20%">
            <el-input v-model="form.name" autocomplete="off" style="width: 90%;"></el-input>
          </el-form-item>
        </el-form>
        <el-form :model="form">
          <el-form-item label="皮肤作者" label-width="20%">
            <el-input v-model="form.author" autocomplete="off" style="width: 90%;"></el-input>
          </el-form-item>
        </el-form>
        <el-form :model="form">
          <el-form-item label="皮肤价格" label-width="20%">
            <el-input v-model="form.price" autocomplete="off" style="width: 90%;"></el-input>
          </el-form-item>
        </el-form>
        <el-form :model="form">
          <el-form-item label="皮肤出版社" label-width="20%">
            <el-input v-model="form.press" autocomplete="off" style="width: 90%;"></el-input>
          </el-form-item>
        </el-form>
        <el-form :model="form">
          <el-form-item label="皮肤介绍" label-width="20%">
            <div id="editor">

            </div>
          </el-form-item>
        </el-form>
        <el-form :model="form">
          <el-form-item label="皮肤封面" label-width="20%">
            <el-upload
                action="http://localhost:8080/api/files/upload"
                :on-success="successUpload">
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
            </el-upload>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";
import E from "wangeditor";

let editor
function initWangEditor(content) {
  setTimeout(() => {
    if (!editor) {
      editor = new E('#editor')
      editor.config.placeholder = '请输入皮肤介绍'
      editor.config.uploadFileName = 'file'
      editor.config.uploadImgServer = 'http://localhost:8080/api/files/upload'
      editor.create()
    }
    editor.txt.html(content)
  }, 0)
}

export default {
  computed: {
    http() {
      return http
    }
  },
  data() {
    return {
      params: {
        name: '',
        author: '',
        pageNum: 1,
        pageSize: 5,
      },
      tableData: [],
      total: 0,
      dialogFormVisible: false,
      form: {},
      formLabelWidth: '80px',
      user: localStorage.getItem('user') ? JSON.parse(localStorage.getItem('user')) : {}
    }
  },

  created() {
    this.findBySearch()
  },

  methods: {
    findBySearch(){
      request.get("/sport/search", {
        params: this.params
      }).then(res =>{
        if(res.code === '0'){
          this.tableData = res.data.list;
          this.total = res.data.total;
        }else{
          this.$message.error(res.msg);
        }
      })
    },

    add(){
      this.form = {};
      initWangEditor('')
      this.dialogFormVisible = true
    },
    edit(obj){
      this.form = obj;
      this.dialogFormVisible = true
    },
    down(flag){
      location.href = "http://localhost:8080/api/files/" + flag.img
    },

    reset(){
      this.params = {
        pageNum: 1,
        pageSize: 5,
        name: '',
        phone: '',
      }
      this.findBySearch();
    },
    handleSizeChange(pageSize){
      this.params.pageSize = pageSize;
      this.findBySearch();
    },
    handleCurrentChange(pageNum){
      this.params.pageSize = pageNum;
      this.findBySearch();
    },
    submit(){
      request.post("/sport", this.form).then(res =>{
        if(res.code === "0"){
          this.$message.success("操作成功");
          this.dialogFormVisible = false;
          this.findBySearch();
        }else{
          this.$message.error(res.msg);
        }
      })
    },
    del(id){
      request.delete("/sport/" + id).then(res => {
        if (res.code === "0") {
          this.$message.success("删除成功");
          this.findBySearch();
        } else {
          this.$message.error(res.msg);
        }
      })
    },
    successUpload(res){
      console.log(res);
      this.form.img = res.data;
    }
  }
}
</script>
